package initialize

import (
	"errors"
	"fmt"
	"gorm.io/driver/mysql"
	"gorm.io/gorm"
	"week2zk-project/rpc/global"
	"week2zk-project/rpc/internal/model"
)

func InitMysql() {
	var err error
	mysqlConfig := global.AppData.Server.Mysql
	dsn := fmt.Sprintf("%s:%s@tcp(%s:%d)/%s", mysqlConfig.Username, mysqlConfig.Password, mysqlConfig.Host, mysqlConfig.Port, mysqlConfig.Database)
	global.DB, err = gorm.Open(mysql.Open(dsn))
	if err != nil {
		panic(errors.New("mysql数据库连接失败"))
		return
	}
	fmt.Println("mysql数据库连接成功")
	global.DB.AutoMigrate(new(model.Goods), new(model.Order))
}
